package model.generator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import model.Course;
import model.CourseHistory;
import model.FreeStudent;
import model.Registration;
import model.RetrainingStudent;
import model.Student;
import model.container.RegistrationContainer;
import model.container.StudentContainer;
import model.generator.enums.Degree;
import model.generator.enums.FirstName;
import model.generator.enums.LastName;
import model.generator.enums.Nationality;
import model.generator.enums.RetrainingReason;

public class StudentGenerator {

	private static Random random = new Random();
	// METHODS
	
	/**
	 * A static Student generator method that generates Students with random
	 * properties.
	 * 
	 * @param count
	 *            The amount of students that should be generated.
	 * @return A Collection of Students with random properties.
	 */
	public static List<Student> generateStudents(int count) throws Exception {
		
		
		
		// Initialize list
		List<Student> students = new ArrayList<Student>();
		
		// Fill list
		for (int i = 0; i < count; i++) {
			Student student = null;
			FirstName[] names = FirstName.values();
			LastName[] lastNames = LastName.values();
			String name = names[random.nextInt(names.length)].getValue() +" " +  lastNames[random.nextInt(lastNames.length)].getValue() ;
			if (random.nextInt(2) == 0) {
				Nationality[] nationalities = Nationality.values();
				student = new FreeStudent("" + i, name, 
						nationalities[random.nextInt(nationalities.length)].getValue());
			} else {
				Degree[] degrees = Degree.values();
				RetrainingReason[] retrainingReasons = RetrainingReason.values(); 
				student = new RetrainingStudent("" + i, name,
						degrees[random.nextInt(degrees.length)].getValue(),
						retrainingReasons[random
								.nextInt(retrainingReasons.length)].getValue());
			}
			students.add(student);
		}
		return students;
	}
	
	/**
	 * Initialize the Student reference fields
	 */
	public static void finishStudentInitialisation(){
		StudentContainer studentContainer = StudentContainer.getInstance();
		RegistrationContainer registrationContainer = RegistrationContainer.getInstance();
		List<Student> students = studentContainer.getStudents();
		//iterate over all the students and set their reference fields
		for(int i=0; i < students.size(); i++){
			//set registrations
			Set<Registration> registrations = registrationContainer.getRegistrationsForStudent(students.get(i));
			students.get(i).setRegistrations(registrations);
			//create a random course history
			Collection<CourseHistory> courseHistories = new ArrayList<CourseHistory>();
			for(Registration registration: registrations){
				courseHistories.add(new CourseHistory(""
						+ registration.getRegistrationDate().getYear(), random.nextInt(20),
						registration.getOfferedCourse().getCourse()));
			}
			students.get(i).setCourseHistories(courseHistories);
		}
	}
	
	public static List<Student> getRandomStudents(){
		StudentContainer studentContainer = StudentContainer.getInstance();
		int toIndex = random.nextInt(studentContainer.getStudentCount());
		int fromIndex = toIndex > 0 ? random.nextInt(toIndex):0;
		return studentContainer.getStudents().subList(fromIndex, toIndex);
	}
}
